(:#############################################################################################

    Einfügen der Komponisten mit UUID aus der Komponisten Liste in die Mediathek.

#############################################################################################:)

import module namespace functx = "http://www.functx.com" at "http://www.xqueryfunctions.com/xq/functx-1.0-nodoc-2007-01.xq"; 

declare function local:parseComposer($composer as xs:string?)
{
    <Composers> {
        if (fn:exists($composer)) then (
            let $normc := fn:normalize-space($composer),
                $validname := "[a-z|A-Z|ü|ä|ö|Ü|Ä|Ö|ß|é|\-|ó|ř|á]+",
                (:Surname, Forename Forename (YearOfBirth-YearOfDeath):)
                $reg1 := fn:concat("^", $validname, ",\s", $validname, "\s", $validname, "\s\(.*\)$"),        
                (:Surname, Forename (YearOfBirth-YearOfDeath):)
                $reg2 := fn:concat("^", $validname, ",\s", $validname, "\s\(.*\)$"),
                (:Forename Forename Surname (YearOfBirth-YearOfDeath):)
                $reg3 := fn:concat("^", $validname, "\s", $validname, "\s", $validname, "\s\(.*\)$"),        
                (:Forename Surname (YearOfBirth-YearOfDeath):)        
                $reg4 := fn:concat("^", $validname, "\s", $validname, "\s\(.*\)$"),        
                (:Forename Forename Surname:)
                $reg5 := fn:concat("^", $validname, "\s", $validname, "\s", $validname),
                (:J.S. Bach:)
                $reg6 := fn:concat("^", $validname, "\.", $validname, "\.\s", $validname, "$"),
                (:Forename Surname:)
                $reg7 := fn:concat("^", $validname, "\s", $validname, "$"),        
                (:Surname:)
                $reg8 := fn:concat("^", $validname, "$"),
                (:Surname, Forename:)
                $reg9 := fn:concat("^", $validname, ",\s", $validname, "$"),        
                (:Surname, Surname, ..., Surname:)
                $reg10 := fn:concat("^(", $validname, ",\s?){2,}\s", $validname, "$"),
                (:Forename Surname/Surname:)
                $reg11 := fn:concat("^", $validname, "\s", $validname, "/", $validname, "$"),
                (:Surname/Forename Surname:)
                $reg12 := fn:concat("^", $validname, "/", $validname, "\s", $validname, "$")
            return 
                if (fn:matches($normc, $reg1))
                (:Surname, Forename Forename (YearOfBirth-YearOfDeath):)
                then <Composer>
                        <Forename>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, ","), "("))}</Forename>
                        <Surname>{fn:normalize-space(fn:substring-before($normc, ","))}</Surname>
                        <YearOfBirth>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "("), "-"))}</YearOfBirth>
                        <YearOfDeath>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "-"), ")"))}</YearOfDeath>
                     </Composer>
                else if (fn:matches($normc, $reg2))
                (:Surname, Forename (YearOfBirth-YearOfDeath):)
                then <Composer>
                        <Forename>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, ","), "("))}</Forename>
                        <Surname>{fn:substring-before($normc, ",")}</Surname>
                        <YearOfBirth>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "("), "-"))}</YearOfBirth>
                        <YearOfDeath>{fn:normalize-space(fn:substring-before(fn:substring-after(fn:substring-after($normc, "("), "-"), ")"))}</YearOfDeath>                
                     </Composer>
                else if (fn:matches($normc, $reg3))
                (:Forename Forename Surname (YearOfBirth-YearOfDeath):)
                then <Composer> {
                            let $name := fn:normalize-space(fn:substring-before($normc, "("))
                            let $Forename1 := fn:substring-before($name, " ")
                            let $Forename2 := fn:substring-before(fn:substring-after($name, " "), " ")
                            let $Surname := fn:substring-after(fn:substring-after($name, " "), " ")
                            return <Forename>{fn:string-join(($Forename1, $Forename2), " ")}</Forename>
                                    union
                                   <Surname>{$Surname}</Surname>
                                   union
                                   <YearOfBirth>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "("), "-"))}</YearOfBirth>
                                   union
                                   <YearOfDeath>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "-"), ")"))}</YearOfDeath>
                    } </Composer>
                else if (fn:matches($normc, $reg4))
                (:Forename Surname (YearOfBirth-YearOfDeath):)
                then <Composer> {
                            let $name := fn:normalize-space(fn:substring-before($normc, "("))
                            return <Forename>{fn:substring-before($name, " ")}</Forename>
                                    union
                                   <Surname>{fn:substring-after($name, " ")}</Surname>
                                   union
                                   <YearOfBirth>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "("), "-"))}</YearOfBirth>
                                   union
                                   <YearOfDeath>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "-"), ")"))}</YearOfDeath>
                    } </Composer>
                else if (fn:matches($normc, $reg5))
                (:Forename Forename Surname:)        
                then <Composer> {
                            let $Forename1 := fn:substring-before($normc, " ")
                            let $Forename2 := fn:substring-before(fn:substring-after($normc, " "), " ")
                            let $Surname := fn:substring-after(fn:substring-after($normc, " "), " ")
                            return <Forename>{fn:concat($Forename1, " ", $Forename2)}</Forename>
                                    union
                                   <Surname>{$Surname}</Surname>
                    } </Composer>                
                else if (fn:matches($normc, $reg6))
                (:J.S. Bach:)        
                then <Composer>
                        <Forename>{fn:substring-before($normc, " ")}</Forename>
                        <Surname>{fn:substring-after($normc, " ")}</Surname>
                     </Composer>
                else if (fn:matches($normc, $reg7))
                (:Forename Surname:)        
                then <Composer>
                        <Forename>{fn:substring-before($normc, " ")}</Forename>
                        <Surname>{fn:substring-after($normc, " ")}</Surname>
                     </Composer>
                else if (fn:matches($normc, $reg8))
                (:Surname:)
                then <Composer>
                        <Surname>{$normc}</Surname>
                     </Composer>
                else if (fn:matches($normc, $reg9))
                (:Surname, Forename:)        
                then <Composer>
                        <Forename>{fn:normalize-space(fn:substring-after($normc, ","))}</Forename>
                        <Surname>{fn:substring-before($normc, ",")}</Surname>
                     </Composer>
                else if (fn:matches($normc, $reg10))
               (:Surname, Surname, ..., Surname:)
                then (
                    for $name in fn:tokenize($normc, ",")
                    return <Composer><Surname>{fn:normalize-space($name)}</Surname></Composer>
                )
                else if (fn:matches($normc, $reg11))
                (:Forename Surname/Surname:)
                then <Composer>
                        <Forename>{fn:substring-before($normc, " ")}</Forename>
                        <Surname>{fn:substring-after($normc, "/")}</Surname>
                    </Composer>
                else if (fn:matches($normc, $reg12))
                (:Surname/Forename Surname:)
                then <Composer>
                        <Forename>{fn:substring-before(fn:normalize-space(fn:substring-after($normc, "/")), " ")}</Forename>
                        <Surname>{fn:substring-before($normc, "/")}</Surname>        
                    </Composer>
                else ()
            ) else ()
        } </Composers>
};

declare function local:parseName($name as xs:string?)
{
    <Composers> {
        if (fn:exists($name)) then (
            let $normc := fn:normalize-space($name),
                $validname := "[a-z|A-Z|ü|ä|ö|Ü|Ä|Ö|ß|é|\-|ó|ř|á]+",
                (:Surname I* Surname:)
                $reg1 := fn:concat("^", $validname, "\sI[I | &amp; | \s]*\s", $validname, "$"),        
                (:Surname: ... (.*):)
                $reg2 := fn:concat("^", $validname, ":\s.*\(.*\)$"),
                (:Surname ... (.*):)
                $reg3 := fn:concat("^", $validname, "\s", $validname, "\s[\d\s\(.*\)$ | I+]"),            
                (:Surname:)
                $reg4 := fn:concat("^", $validname, "\s?", $validname, "?$"),                
                (:Surname, Forename: ... (...):)
                $reg5 := fn:concat("^", $validname, ",\s", $validname, ":.*\(.*\)$"),            
                (:Surname (Initialen): ...:)
                $reg6 := fn:concat("^", $validname, "\s\(", $validname, "\.?", $validname, "\.?\):"),
                (:Surname: ...:)
                $reg7 := fn:concat("^", $validname, ":\s(", $validname, "|\s?)*$"),
                (:Surname: ... - Name:)
                $reg8 := fn:concat("^", $validname, ":\s.*\s\-\s(\d\.\s)?", $validname, ".*$"),
                (:Initialen.Initialien.Surname /:)
                $reg9 := fn:concat("^", $validname, "\.\s?", $validname, "\.\s?", $validname, "\s[/ | \-]{1}.*"),
                (:Surname - ...:)
                $reg10 := fn:concat("^", $validname, "\s\-.*$"),
                (:Initialen. Surname - ...:)
                $reg11 := fn:concat("^", $validname, "\.\s?", $validname, "\s\-.*$"),
                (:Surname/Surname: ...:)
                $reg12 := fn:concat("^", $validname, "/\s?", $validname, "\s?:.*$"),
                (:Name Name Name Name \d (Nachname):)
                $reg13 := fn:concat("^", $validname, "\s", $validname, "\s", $validname, "\s", $validname, "\s(\d\s)?\(", $validname, "\)$"),
                (:Name Name Name Nachname \d (.*):)
                $reg14 := fn:concat("^", $validname, "\s", $validname, "\s", $validname, "\s\d\s.*\(.*\)$")
            return
                if (fn:matches($normc, $reg1))
                (:Surname I* Surname:)
                then <Composer>
                        <Surname>{fn:normalize-space(fn:substring-before($normc, "I"))}</Surname>
                        <reg1/>
                     </Composer>
                else if (fn:matches($normc, $reg2))
                (:Surname: ... (.*):)
                then <Composer>
                        <Surname>{fn:substring-before($normc, ":")}</Surname>
                        <reg2/>
                     </Composer>
                else if (fn:matches($normc, $reg3) or fn:matches($normc, $reg14))
                (:Surname ... (.*):)
                then <Composer>
                        <Surname>{fn:substring-before($normc, " ")}</Surname>
                        <reg3/>
                     </Composer>
                else if (fn:matches($normc, $reg4))
                (:Surname:)
                then <Composer>
                        <Surname>{$name}</Surname>
                        <reg4/>
                     </Composer>
                else if (fn:matches($normc, $reg5))
                (:Surname, Forename: ... (...):)        
                then <Composer>
                        <Forename>{fn:normalize-space(fn:substring-after(fn:substring-before($name, ":"), ","))}</Forename>
                        <Surname>{fn:normalize-space(fn:substring-before($name, ","))}</Surname>
                        <reg5/>
                     </Composer>                
                else if (fn:matches($normc, $reg6))
                (:Surname (Initialen): ...:)        
                then <Composer>
                        <Forename>{
                            let $name := fn:substring-after(fn:substring-before($normc, ")"), "("),
                                $forename := (
                                    for $char in functx:chars($name)
                                    return fn:concat($char, ".")
                                )
                            return fn:replace(fn:string-join($forename, ""), " ", "")
                        }</Forename>
                        <Surname>{fn:substring-before($normc, " ")}</Surname>
                        <reg6/>
                     </Composer>
                else if (fn:matches($normc, $reg7) or fn:matches($normc, $reg8))
                (:Surname: ...:)                
                then <Composer>
                        <Surname>{fn:substring-before($normc, ":")}</Surname>
                        <reg7/>
                     </Composer>
                else if (fn:matches($normc, $reg9))
                (:Initialen.Initialien.Surname /:)                
                then <Composer>
                        <Forename>{fn:concat(fn:substring-before($normc, "."), ".",
                                             fn:substring-before(fn:substring-after($normc, "."), "."), ".")}</Forename>
                        <Surname>{fn:substring-before(fn:normalize-space(fn:substring-after(fn:substring-after($normc, "."), ".")), " ")}</Surname>
                        <reg9/>
                     </Composer>
                else if (fn:matches($normc, $reg10))
                then <Composer>
                        <Surname>{fn:substring-before($normc, " -")}</Surname>
                        <reg10/>
                     </Composer>                        
                else if (fn:matches($normc, $reg11))
                then <Composer>
                        <Forename>{fn:concat(fn:substring-before($normc, "."), ".")}</Forename>
                        <Surname>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "."), "-"))}</Surname>
                        <reg11/>
                     </Composer>
                else if (fn:matches($normc, $reg12))
                then <Composer>
                        <Surname>{fn:normalize-space(fn:substring-before($normc, "/"))}</Surname>
                        <reg12/>
                     </Composer>                 
                else if (fn:matches($normc, $reg13))
                then <Composer>
                        <Surname>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, "("), ")"))}</Surname>
                        <reg13/>
                     </Composer>                 
                else ()
            ) else ()
        }</Composers>
};

declare function local:parseAlbum($album as xs:string?)
{
    <Composers> {
        if (fn:exists($album)) then (
            let $normc := fn:normalize-space($album),
                $validname := "[a-z|A-Z|ü|ä|ö|Ü|Ä|Ö|ß|é|\-|ó|ř|á]+",
                (:Surname: ...:)
                $reg1 := fn:concat("^", $validname, ":.*$"),        
                (:Initialen. Initialen. Surname - ... (Disc 2):)
                $reg2 := fn:concat("^", $validname, "\.\s", $validname, "\.\s", $validname, "\s-\s.*\(", $validname, "\s\d\)$"),
                (:Surname (Initialen Initialen): ... [Disc 2]:)
                $reg3 := fn:concat("^", $validname, "\s\(", $validname, "\):.*\[", $validname, "\s\d\]$"),            
                (:Surname; Surname: ...:)
                $reg4 := fn:concat("^", $validname, ";\s", $validname, ":\s.*$"),                
                (:Wir entdecken Komponisten: Johannes Brahms:)
                $reg5 := fn:concat("^", $validname, "\s", $validname, "\s", $validname, ":\s", $validname, "\s", $validname, "?$"),            
                (:Juwelen Der Inspiration: Die Klavierkonzerte Von Mozart:)
                $reg6 := fn:concat("^", $validname, "\s", $validname, "\s", $validname, ":\s", $validname, "\s", $validname, "\s", $validname, "?\s", $validname, "?$"),
                (:Initialen.Initialen.Surname / ...:)
                $reg7 := fn:concat("^", $validname, "\.", $validname, "\.", $validname, "\s/\s"),
                (:Isaac Stern - A Life In Music - Beethoven - Brahms: Violin Concertos - Triple Concerto - Double Concerto:)
                $reg8 := fn:concat("^.*\s\-\s.*\s\-\s", $validname ,"\s\-\s", $validname,":\s.*\s\-\s.*\s\-\s.*$"),
                (:Works By Fritz Kreisler (Gaede, Moll):)
                $reg9 := fn:concat("^", $validname, "\s", $validname, "\s", $validname, "\s", $validname, "\s\(.*\)$")
            return
                if (fn:matches($normc, $reg1))
                (:Surname: ...:)            
                then <Composer>
                        <Surname>{fn:normalize-space(fn:substring-before($normc, ":"))}</Surname>
                        <reg1/>
                     </Composer>
                else if (fn:matches($normc, $reg2))
                (:Initialen. Initialen. Surname - ... (Disc 2):)                       
                then <Composer>
                        <Forename>{fn:concat(fn:normalize-space(fn:substring-before($normc, " ")),
                                             fn:normalize-space(fn:substring-before(fn:substring-after($normc, " "), " ")))}</Forename>
                        <Surname>{fn:normalize-space(fn:substring-before(fn:substring-after(fn:substring-after($normc, "."), "."), "-"))}</Surname>
                        <reg2/>
                     </Composer>
                else if (fn:matches($normc, $reg3))
                (:Surname (Initialen Initialen): ... [Disc 2]:)            
                then <Composer>
                        <Forename>{
                            let $initialen := fn:normalize-space(fn:substring-before(fn:substring-after($normc, "("), ")"))
                            return (for $char in functx:chars($initialen) return fn:concat($char, "."))
                        }</Forename>
                        <Surname>{fn:substring-before($normc, " ")}</Surname>
                        <reg3/>
                     </Composer>
                else if (fn:matches($normc, $reg4))
                (:Surname; Surname: ...:)
                then <Composer><Surname>{fn:substring-before($normc, ";")}</Surname></Composer>
                     union
                     <Composer>
                         <Surname>{fn:normalize-space(fn:substring-before(fn:substring-after($normc, ";"), ":"))}</Surname>
                         <reg4/>
                     </Composer>
                else if (fn:matches($normc, $reg5))
                (:Wir entdecken Komponisten: Johannes Brahms:)
                then <Composer>
                        <Forename>{fn:substring-before(fn:normalize-space(fn:substring-after($normc, ":")), " ")}</Forename>
                        <Surname>{fn:substring-after(fn:normalize-space(fn:substring-after($normc, ":")), " ")}</Surname>
                        <reg5/>
                     </Composer>                
                else if (fn:matches($normc, $reg6))
                (:Juwelen Der Inspiration: Die Klavierkonzerte Von Mozart:)
                then <Composer>
                        <Surname>{fn:tokenize($normc, " ")[fn:position() = fn:last()]}</Surname>
                        <reg6/>
                     </Composer>
                else if (fn:matches($normc, $reg7))
                (:Initialen.Initialen.Surname / ...:)
                then <Composer>{
                        let $name := fn:normalize-space(fn:substring-before($normc, "/"))
                        return <Forename>{fn:concat(fn:substring-before($name, "."), ".",
                                                    fn:substring-before(fn:substring-after($name, "."), "."), ".")
                                         }</Forename>
                               union
                               <Surname>{fn:substring-after(fn:substring-after($name, "."), ".")}</Surname>
                               union
                               <reg7/>
                    }</Composer>
                else if (fn:matches($normc, $reg8))
                (:Isaac Stern - A Life In Music - Beethoven - Brahms: Violin Concertos - Triple Concerto - Double Concerto:)            
                then (
                    let $name := fn:normalize-space(fn:substring-after(fn:substring-after(fn:substring-before($normc, ":"), "-"), "-"))
                    return <Composer><Surname>{fn:normalize-space(fn:substring-before($name, "-"))}</Surname></Composer>
                           union
                           <Composer><Surname>{fn:normalize-space(fn:substring-after($name, "-"))}</Surname></Composer>
                           union
                           <reg8/>
                     )
                (:Works By Fritz Kreisler (Gaede, Moll):)                 
                else if (fn:matches($normc, $reg9))
                then <Composer> {
                        let $name := fn:normalize-space(fn:substring-after(fn:substring-after(fn:substring-before($normc, "("), " "), " "))
                        return <Forename>{fn:normalize-space(fn:substring-before($name, " "))}</Forename>
                                union
                               <Surname>{fn:normalize-space(fn:substring-after($name, " "))}</Surname>
                               union
                               <reg9/>
                     }</Composer>
                else ()
            ) else ()
        }</Composers>
};

declare function local:normalize-name($name as xs:string?)
{
    let $norm := fn:lower-case(fn:normalize-space($name)),
        $norm := fn:replace($norm, "ü", "ue"),
        $norm := fn:replace($norm, "ä", "ae"),
        $norm := fn:replace($norm, "æ", "ae"),
        $norm := fn:replace($norm, "ö", "oe"),
        $norm := fn:replace($norm, "ß", "ss"),
        $norm := fn:replace($norm, "é", "e"),
        $norm := fn:replace($norm, "è", "e"),
        $norm := fn:replace($norm, "ë", "e"),
        $norm := fn:replace($norm, "ó", "o"),
        $norm := fn:replace($norm, "ő", "o"),
        $norm := fn:replace($norm, "'", " "),
        $norm := fn:replace($norm, "`", " "),
        $norm := fn:replace($norm, "ï", "i"),
        $norm := fn:replace($norm, "í", "i"),
        $norm := fn:replace($norm, "á", "a"),
        $norm := fn:replace($norm, "ç", "c"),
        $norm := fn:replace($norm, "ł", "l"),
        $norm := fn:replace($norm, "ÿ", "y"),
        $norm := fn:replace($norm, "zehetmaier", "zehetmair"),
        $norm := fn:replace($norm, "tchaikovsky", "tschaikowski"),
        $norm := fn:replace($norm, "prokofiev", "prokofjew"),
        $norm := fn:replace($norm, "glazunov", "glasunow"),
        $norm := fn:replace($norm, "rachmaninoff", "rachmaninow"),
        $norm := fn:replace($norm, "rachmaninov", "rachmaninow"),
        $norm := fn:replace($norm, "stravinsky", "strawinski"),
        $norm := fn:replace($norm, "rimsky-korsakov", "rimski-korsakow"),
        $norm := fn:replace($norm, "peter ilyich", "peter iljitsch")
    return $norm
};

declare function local:getNextName($name as xs:string) as xs:string
{
  if (fn:contains($name, " ")) then
    if (fn:contains($name, ".")) then
      if (fn:string-length(fn:substring-before($name, ".")) < fn:string-length(fn:substring-before($name, " "))) then
        fn:concat(fn:substring-before($name, "."), ".")
      else
        fn:substring-before($name," ")
    else
      fn:substring-before($name," ")
  else
    if (fn:contains($name, ".")) then
      fn:concat(fn:substring-before($name,"."), ".")
    else
      $name
};

declare function local:recCompareNames($name1 as xs:string, $name2 as xs:string) as xs:boolean
{
    if (fn:string-length($name1) = 0 and fn:string-length($name2) = 0) then
        fn:true()
    else
       if (fn:string-length($name1) = 0 or fn:string-length($name2) = 0) then
	       fn:false()
       else
	       let $surname1 := local:getNextName($name1),
	           $nextName1 := fn:normalize-space(fn:substring-after($name1, $surname1)),
	           $surname2 := local:getNextName($name2),
	           $nextName2 := fn:normalize-space(fn:substring-after($name2, $surname2))
	       return
	           if (fn:contains($surname1, ".")) then
	               fn:starts-with($surname2, fn:substring-before($surname1, ".")) and local:recCompareNames($nextName1, $nextName2)
	           else
	               $surname1 = $surname2 and local:recCompareNames($nextName1, $nextName2)
};

declare function local:compareNames($name1 as node()?, $name2 as node()?) as xs:boolean
{
    if (fn:exists($name1) and fn:exists($name2)) then
      local:recCompareNames(local:normalize-name($name1/text()), local:normalize-name($name2/text()))
    else
        fn:false()
};

(:Funktion, die aufgrund der Ergebnisse der Parser, die Komponisten vergleicht:)
declare function local:compareComposers($composer1 as node(), $composer2 as node()) as xs:boolean
{
    let $equalSurname := local:compareNames($composer1/Surname, $composer2/Surname),
        $equalForename := local:compareNames($composer1/Forename, $composer2/Forename),
        $forenameSurname1 := fn:concat($composer1/Forename/text(), " ", $composer1/Surname/text()),
        $forenameSurname2 := fn:concat($composer2/Forename/text(), " ", $composer2/Surname/text()),
        $equalForenameSurname := local:compareNames(<Surname>{$forenameSurname1}</Surname>, <Surname>{$forenameSurname2}</Surname>),
        $equalYearOfBirth := fn:exists($composer1/YearOfBirth) and fn:exists($composer2/YearOfBirth) and
                             fn:normalize-space($composer1/YearOfBirth) = fn:normalize-space($composer2/YearOfBirth),
        $equalYearOfDeath := fn:exists($composer1/YearOfDeath) and fn:exists($composer2/YearOfDeath) and
                             fn:normalize-space($composer1/YearOfDeath) = fn:normalize-space($composer2/YearOfDeath),                             
        $rule1 := $equalSurname and $equalForename,
        $rule2 := $equalSurname and $equalYearOfBirth and $equalYearOfDeath,
        $rule3 := $equalForename and $equalYearOfBirth and $equalYearOfDeath,
        $rule4 := fn:not($equalForename or $equalSurname) and $equalForenameSurname,
        $rule5 := fn:not(fn:exists($composer1/Forename)) and $equalSurname,
        $rule6 := fn:not($equalSurname) and ((fn:starts-with($composer2/Surname, $composer1/Surname) and fn:contains($composer2/Surname, " ")) or
                                            (fn:starts-with($composer1/Surname, $composer2/Surname) and fn:contains($composer1/Surname, " "))),
        $rule7 := $equalSurname and ((fn:starts-with($composer2/Forename, $composer1/Forename) and fn:contains($composer2/Forename, " ")) or
                                    (fn:starts-with($composer1/Forename, $composer2/Forename) and fn:contains($composer1/Forename, " ")))
    return $rule1 or $rule2 or $rule3 or $rule4 or $rule5 or $rule6 or $rule7
};

declare function local:validComposer($param as node())
{
    <Composers> {
        for $composer1 in $param//Composer
        return <Assignments>{
            <OldComposer>{$composer1/*}</OldComposer>
            union (
            for $composer2 in doc("Komponisten_Test.xml")/Composers/Composer
            (:for $composer2 in doc("../Aufgabe2/KomponistenUUID.xml")/Composers/Composer:)
            where local:compareComposers($composer1, $composer2)
            (:return $composer2/ComposerUUID):)
            return $composer2)(:Test:)
        } </Assignments>
    }</Composers>
};

for $mediathek in doc("../Rohdaten_Mediathek/Mediathek.xml")/Mediathek
return <Mediathek>{
    (:($mediathek/MetaData)
    union:)
    (for $trackarea in $mediathek/TrackArea
    return <TrackArea>{
        for $track in $trackarea/Track
        return <Track>{
            let $validComposer := local:parseComposer($track/Composer),
                $validName := local:parseName($track/Name),
                $validAlbum := local:parseAlbum($track/Album)
            return (
                if (fn:exists($track/Composer)) then
                    local:validComposer($validComposer)
                else if (fn:exists($track/Album) and fn:count($validAlbum/Composer) > 0) then
                    local:validComposer($validAlbum)
                else if (fn:exists($track/Name) and fn:count($validName/Composer) > 0) then
                    local:validComposer($validName)                
                else ($track/Composer union $track/Album union $track/Name)
            )
            (:union
            $track/*:)
        }</Track>
    }</TrackArea>)
    (:union
    ($mediathek/Playlists):)
}</Mediathek>
